import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

/**
 * Created by lrs on 2017/12/30.
 */
public class Handle {
    /**
     * 使用文本查询的方式处理
     * 例如：<div class="top-main">xxxxx</div></div> 文本需要替换则设置
     *  txtHandle.setStartStr("<div class=\"top-main\">");
        txtHandle.setStartNum(1); // 第一个出现<div class="top-main">的地方
        txtHandle.setEndStr("</div>");
        txtHandle.setEndNum(2); // 第一个出现</div>的地方
        txtHandle.setReplaceStartStrAndEndStr(true); // 替换后不包括<div class="top-main">xxxxx和第二个</div>
     * @param source 原目录
     * @param target 目标目录
     * @param txtHandleList
     */
    public void handleFile(String source, String target, LinkedList<TxtHandle> txtHandleList) {
        String sourceStr = HandleFile.readFile(source); // 读取待处理字符串

        for (TxtHandle txtHandle : WebReplaceText.txtHandleList) {
            sourceStr = this.handleFile(sourceStr, txtHandle);
        }

        try {
            this.writeFile(target, sourceStr); // 写入字符到目标文件中
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理一组“使用文本查询的方式处理”
     * @param sourceStr
     * @param txtHandle
     * @return
     */
    private String handleFile(String sourceStr, TxtHandle txtHandle) {
        int startIndex = 0;
        for (int i=0; i<txtHandle.getStartNum(); i++) {
            startIndex = sourceStr.indexOf(txtHandle.getStartStr(), startIndex+1);
        }

        int endIndex = startIndex;
        for (int i=0; i<txtHandle.getEndNum(); i++) {
            endIndex = sourceStr.indexOf(txtHandle.getEndStr(), endIndex+1);
        }

        String str = sourceStr;
        if (startIndex!=-1 && endIndex!=-1) {
            if (txtHandle.isReplaceStartStrAndEndStr()) {
                str = sourceStr.substring(0, startIndex) + txtHandle.getReplaceText() + sourceStr.substring(endIndex+txtHandle.getEndStr().length(), sourceStr.length());
            } else {
                str = sourceStr.substring(0, startIndex + txtHandle.getStartStr().length()) + txtHandle.getReplaceText() + sourceStr.substring(endIndex, sourceStr.length());
            }
        }

        return str;
    }


    /**
     * 处理一个目录
     * @param file File对象
     * @param source
     * @param target
     */
    public void directory(File file, String source, String target) {
        String fullDir = file.getAbsolutePath().replace("\\", "/"); // 全路径
        String relativeDir = fullDir.replace(source, "");// 相对路径
        String fullTargetDir = target + "/" + relativeDir; // 目标全目标路径

        // 目标目录不存在则创建
        File targetDir = new File(fullTargetDir);
        HandleFile.judeDirExists(targetDir);
    }

    /**
     * 将处理好的字符串写入目标文件
     * @param target
     * @param fileStr
     * @throws Exception
     */
    private void writeFile(String target, String fileStr) throws IOException {
        // 写入字符到目标文件中
        FileWriter fout = new FileWriter(target);// 创建文件输出流
        fout.write(fileStr.toCharArray());// 把替换完成的字符串写入文件内
        fout.close();// 关闭输出流
    }

}
